# -*- coding: utf-8 -*-
# @Date    : 2015-06-21 15:50:35
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

from lib.tornado_routes import route
from web.handler import BaseHandler
from tornado import gen
from tornado.web import asynchronous
from web.models.admin.repair.type import Type
from web.models.admin.user.student import Student
from web.models.admin.repair.log import Log
import json
import time
import md5
import base64
from lib.app import libtype
from lib import libtime,libpage,libmd5
from web.models.admin.user.repairman import Repairman

@route(r'/admin/repair/log/?')
class IndexHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        cur_page = self.get_argument('page', default=1)
        school_id = self.get_argument('schoolId', default=self.school_id)

        school = self.get_cookie('school', base64.b16encode('{}'))
        school = json.loads(base64.b16decode(school))
        dt = school.get('log', {})
        sname = dt.get('sname', '')
        sno = dt.get('sno', '')
        sdorm = dt.get('sdorm', '')
        sstatus = dt.get('sstatus', '')

        where = {
            "Fname":   sname,
            "Fno":     sno,
            "Fdorm_id": sdorm,
            "Fstatus": sstatus,
        }

        page = (int(cur_page) - 1) * self.offset

        drom = yield Student().select_dorm(school_id)
        # itype = yield Type().select_all_type(school_id)
        itype = libtype.get_type_dict()
        ret, count = yield Log().select_log(school_id, where, page, self.offset)
        repairman = yield Repairman().list(school_id, 0, 200)

        page_info = {'total': count, 'page': int(cur_page)}
        self.render("admin/repair/log/list.html", data=ret, where=where, drom=drom, itype=itype, page_info=page_info, repairman=repairman[1])

@route(r'/admin/repair/log/save/?')
class SaveHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def post(self):
        page = self.get_argument('page',default='1')
        did = self.get_argument('did',default='')
        mainid = self.get_argument('mainid',default='')

        yield Log().save(did, mainid)

        self.redirect('/admin/repair/log/?page='+page)

@route(r'/admin/repair/log/finish/?')
class FinishHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        page = self.get_argument('page',default='1')
        ids = self.get_argument('ids',default='')

        yield Log().finish(ids)

        self.redirect('/admin/repair/log/?page='+page)


@route(r'/admin/repair/log/search/?')
class SearchHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        student_name = self.get_argument('studentName',default='')
        tel = self.get_argument('tel',default='')
        repairman = self.get_argument('repairmanName', default='')
        work_id = self.get_argument('workType',default='')
        status = self.get_argument('status', default='')
        dorm_no = self.get_argument('dormNo', default='')
        school_id = self.get_argument('schoolId', default=self.school_id)

        cur_page = self.get_argument('page', default=1)
        offset = self.get_argument('offset', default=self.offset)
        start = libpage.start(cur_page, offset)

        result = yield Log().search(school_id, student_name, tel, repairman, work_id, status, dorm_no, start, offset)
        man_list = []
        if len(result) > 0:
            page_info = {'total':result[0], 'page':int(cur_page)}
            man_list = result[1]
        else:
            page_info = {'total':0, 'page':0}
        print man_list
        ser_list = [student_name, tel,repairman, work_id,status,dorm_no]

        type_list = libtype.get_type_dict()


        drom = yield Student().select_dorm(school_id)

        where = {
            "Fname":   repairman,
            "Fno":     tel,
            "Fdorm_id": dorm_no,
            "Fstatus": status,
        }

        self.render("admin/repair/log/list.html",page_info=page_info,drom=drom, itype=type_list, data=man_list, where=where)



